#!/usr/bin/perl
use strict;
use warnings;

my ($x, $y);  # placeholders
my ($yyyy, $mm, $dd, $HH, $MM, $SS, $ampm); # date string elements

while (<>) {
$x = $_;
# reformat the date string to be mysqlish
	if ($x =~ s/(^Sent\s*: \w*,) (\w*) (\w*), ([[:digit:]]{4}) (\w{2}):(\w{2}):(\w{2}) (\w{2}).*$/$4-$3-$2 $5:$6:$7 $8/) {

		# convert month-words to month-numbers
		$x =~ s/Jan(uary)?/01/i;
		$x =~ s/Feb(ruary)?/02/i;
		$x =~ s/Mar(ch)?/03/i;
		$x =~ s/Apr(il)?/04/i;
		$x =~ s/May/05/i;
		$x =~ s/Jun(e)?/06/i;
		$x =~ s/Jul(y)?/07/i;
		$x =~ s/Aug(ust)?/08/i;
		$x =~ s/Sep(tember)?/09/i;
		$x =~ s/Oct(ober)?/10/i;
		$x =~ s/Nov(ember)?/11/i;
		$x =~ s/Dec(ember)?/12/i;

		# convert PM to +12
		$y = $x;
		$y =~ m/([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2}) ([[:digit:]]{2}):([[:digit:]]{2}):([[:digit:]]{2}) (\w{2})/;
		$yyyy = $1;
		$mm = $2;
		$dd = $3;
		$HH = $4;
		$MM = $5;
		$SS = $6;
		$ampm = $7;

		$HH = $HH + 12 if (($ampm =~ m/pm/i) and ($HH < 12));

		# convert midnight to 0 o'clock
		$HH = "00" if (($ampm =~ m/am/i) and ($HH == 12));

		print "$yyyy-$mm-$dd $HH:$MM:$SS\n";
	} else {
		print "$x";
	}
}


